package com.rt.schedulenew.utils.coder;

import java.io.*;
import java.util.Arrays;

public class BASE64Coder {
    private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
    private static final char[] PEM_ARRAY = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ+/".toCharArray();
    private static final byte[] PEM_CONVERT_ARRAY = new byte[256];
    private byte[] decodeBuffer = new byte[4];
    private static final int[] IA = new int[256];

    public BASE64Coder() {
    }

    public static final char[] encodeToChar(byte[] sArr, boolean lineSep) {
        int sLen = sArr != null ? sArr.length : 0;
        if (sLen == 0) {
            return new char[0];
        } else {
            int eLen = sLen / 3 * 3;
            int cCnt = (sLen - 1) / 3 + 1 << 2;
            int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0);
            char[] dArr = new char[dLen];
            int left = 0;
            int d = 0;
            int cc = 0;

            while(left < eLen) {
                int i = (sArr[left++] & 255) << 16 | (sArr[left++] & 255) << 8 | sArr[left++] & 255;
                dArr[d++] = CA[i >>> 18 & 63];
                dArr[d++] = CA[i >>> 12 & 63];
                dArr[d++] = CA[i >>> 6 & 63];
                dArr[d++] = CA[i & 63];
                if (lineSep) {
                    ++cc;
                    if (cc == 19 && d < dLen - 2) {
                        dArr[d++] = '\r';
                        dArr[d++] = '\n';
                        cc = 0;
                    }
                }
            }

            left = sLen - eLen;
            if (left > 0) {
                d = (sArr[eLen] & 255) << 10 | (left == 2 ? (sArr[sLen - 1] & 255) << 2 : 0);
                dArr[dLen - 4] = CA[d >> 12];
                dArr[dLen - 3] = CA[d >>> 6 & 63];
                dArr[dLen - 2] = left == 2 ? CA[d & 63] : 61;
                dArr[dLen - 1] = '=';
            }

            return dArr;
        }
    }

    public static final byte[] decode(char[] sArr) {
        int sLen = sArr != null ? sArr.length : 0;
        if (sLen == 0) {
            return new byte[0];
        } else {
            int sepCnt = 0;

            int pad;
            for(pad = 0; pad < sLen; ++pad) {
                if (IA[sArr[pad]] < 0) {
                    ++sepCnt;
                }
            }

            if ((sLen - sepCnt) % 4 != 0) {
                return null;
            } else {
                pad = 0;
                int len = sLen;

                while(len > 1) {
                    --len;
                    if (IA[sArr[len]] > 0) {
                        break;
                    }

                    if (sArr[len] == '=') {
                        ++pad;
                    }
                }

                len = ((sLen - sepCnt) * 6 >> 3) - pad;
                byte[] dArr = new byte[len];
                int s = 0;
                int d = 0;

                while(d < len) {
                    int i = 0;

                    for(int j = 0; j < 4; ++j) {
                        int c = IA[sArr[s++]];
                        if (c >= 0) {
                            i |= c << 18 - j * 6;
                        } else {
                            --j;
                        }
                    }

                    dArr[d++] = (byte)(i >> 16);
                    if (d < len) {
                        dArr[d++] = (byte)(i >> 8);
                        if (d < len) {
                            dArr[d++] = (byte)i;
                        }
                    }
                }

                return dArr;
            }
        }
    }

    public static final byte[] decodeFast(char[] sArr) {
        int sLen = sArr.length;
        if (sLen == 0) {
            return new byte[0];
        } else {
            int sIx = 0;

            int eIx;
            for(eIx = sLen - 1; sIx < eIx && IA[sArr[sIx]] < 0; ++sIx) {
            }

            while(eIx > 0 && IA[sArr[eIx]] < 0) {
                --eIx;
            }

            int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0;
            int cCnt = eIx - sIx + 1;
            int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
            int len = ((cCnt - sepCnt) * 6 >> 3) - pad;
            byte[] dArr = new byte[len];
            int d = 0;
            int i = 0;
            int r = len / 3 * 3;

            while(d < r) {
                 i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
                dArr[d++] = (byte)(i >> 16);
                dArr[d++] = (byte)(i >> 8);
                dArr[d++] = (byte)i;
                if (sepCnt > 0) {
                    ++i;
                    if (i == 19) {
                        sIx += 2;
                        i = 0;
                    }
                }
            }

            if (d < len) {
                i = 0;

                for(r = 0; sIx <= eIx - pad; ++r) {
                    i |= IA[sArr[sIx++]] << 18 - r * 6;
                }

                for(r = 16; d < len; r -= 8) {
                    dArr[d++] = (byte)(i >> r);
                }
            }

            return dArr;
        }
    }

    public static final byte[] encodeToByte(byte[] sArr, boolean lineSep) {
        int sLen = sArr != null ? sArr.length : 0;
        if (sLen == 0) {
            return new byte[0];
        } else {
            int eLen = sLen / 3 * 3;
            int cCnt = (sLen - 1) / 3 + 1 << 2;
            int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0);
            byte[] dArr = new byte[dLen];
            int left = 0;
            int d = 0;
            int cc = 0;

            while(left < eLen) {
                int i = (sArr[left++] & 255) << 16 | (sArr[left++] & 255) << 8 | sArr[left++] & 255;
                dArr[d++] = (byte)CA[i >>> 18 & 63];
                dArr[d++] = (byte)CA[i >>> 12 & 63];
                dArr[d++] = (byte)CA[i >>> 6 & 63];
                dArr[d++] = (byte)CA[i & 63];
                if (lineSep) {
                    ++cc;
                    if (cc == 19 && d < dLen - 2) {
                        dArr[d++] = 13;
                        dArr[d++] = 10;
                        cc = 0;
                    }
                }
            }

            left = sLen - eLen;
            if (left > 0) {
                d = (sArr[eLen] & 255) << 10 | (left == 2 ? (sArr[sLen - 1] & 255) << 2 : 0);
                dArr[dLen - 4] = (byte)CA[d >> 12];
                dArr[dLen - 3] = (byte)CA[d >>> 6 & 63];
                dArr[dLen - 2] = left == 2 ? (byte)CA[d & 63] : 61;
                dArr[dLen - 1] = 61;
            }

            return dArr;
        }
    }

    public static final byte[] decode(byte[] sArr) {
        int sLen = sArr.length;
        int sepCnt = 0;

        int pad;
        for(pad = 0; pad < sLen; ++pad) {
            if (IA[sArr[pad] & 255] < 0) {
                ++sepCnt;
            }
        }

        if ((sLen - sepCnt) % 4 != 0) {
            return null;
        } else {
            pad = 0;
            int len = sLen;

            while(len > 1) {
                --len;
                if (IA[sArr[len] & 255] > 0) {
                    break;
                }

                if (sArr[len] == 61) {
                    ++pad;
                }
            }

            len = ((sLen - sepCnt) * 6 >> 3) - pad;
            byte[] dArr = new byte[len];
            int s = 0;
            int d = 0;

            while(d < len) {
                int i = 0;

                for(int j = 0; j < 4; ++j) {
                    int c = IA[sArr[s++] & 255];
                    if (c >= 0) {
                        i |= c << 18 - j * 6;
                    } else {
                        --j;
                    }
                }

                dArr[d++] = (byte)(i >> 16);
                if (d < len) {
                    dArr[d++] = (byte)(i >> 8);
                    if (d < len) {
                        dArr[d++] = (byte)i;
                    }
                }
            }

            return dArr;
        }
    }

    public static final byte[] decodeFast(byte[] sArr) {
        int sLen = sArr.length;
        if (sLen == 0) {
            return new byte[0];
        } else {
            int sIx = 0;

            int eIx;
            for(eIx = sLen - 1; sIx < eIx && IA[sArr[sIx] & 255] < 0; ++sIx) {
            }

            while(eIx > 0 && IA[sArr[eIx] & 255] < 0) {
                --eIx;
            }

            int pad = sArr[eIx] == 61 ? (sArr[eIx - 1] == 61 ? 2 : 1) : 0;
            int cCnt = eIx - sIx + 1;
            int sepCnt = sLen > 76 ? (sArr[76] == 13 ? cCnt / 78 : 0) << 1 : 0;
            int len = ((cCnt - sepCnt) * 6 >> 3) - pad;
            byte[] dArr = new byte[len];
            int d = 0;
            int cc = 0;
            int eLen = len / 3 * 3;

            int r;
            while(d < eLen) {
                r = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
                dArr[d++] = (byte)(r >> 16);
                dArr[d++] = (byte)(r >> 8);
                dArr[d++] = (byte)r;
                if (sepCnt > 0) {
                    ++cc;
                    if (cc == 19) {
                        sIx += 2;
                        cc = 0;
                    }
                }
            }

            if (d < len) {
                int i = 0;

                for(eLen = 0; sIx <= eIx - pad; ++eLen) {
                    for(r = 16; d < len; r -= 8) {
                        dArr[d++] = (byte)(i >> r);
                    }
                }
            }

            return dArr;
        }
    }

    public static final String encodeToString(byte[] sArr, boolean lineSep) {
        return new String(encodeToChar(sArr, lineSep));
    }

    public static final String encode(byte[] sArr) {
        return new String(encodeToChar(sArr, false));
    }

    public static final String encode(String s) {
        try {
            return new String(encodeToChar(s.getBytes("UTF-8"), false));
        } catch (UnsupportedEncodingException var2) {
            System.err.println("Base64 encoding error: " + var2.getMessage());
            var2.printStackTrace();
            return null;
        }
    }

    public static final byte[] decode(String str, boolean used) {
        int sLen = str != null ? str.length() : 0;
        if (sLen == 0) {
            return new byte[0];
        } else {
            int sepCnt = 0;

            int pad;
            for(pad = 0; pad < sLen; ++pad) {
                if (IA[str.charAt(pad)] < 0) {
                    ++sepCnt;
                }
            }

            if ((sLen - sepCnt) % 4 != 0) {
                return null;
            } else {
                pad = 0;
                int len = sLen;

                while(len > 1) {
                    --len;
                    if (IA[str.charAt(len)] > 0) {
                        break;
                    }

                    if (str.charAt(len) == '=') {
                        ++pad;
                    }
                }

                len = ((sLen - sepCnt) * 6 >> 3) - pad;
                byte[] dArr = new byte[len];
                int s = 0;
                int d = 0;

                while(d < len) {
                    int i = 0;

                    for(int j = 0; j < 4; ++j) {
                        int c = IA[str.charAt(s++)];
                        if (c >= 0) {
                            i |= c << 18 - j * 6;
                        } else {
                            --j;
                        }
                    }

                    dArr[d++] = (byte)(i >> 16);
                    if (d < len) {
                        dArr[d++] = (byte)(i >> 8);
                        if (d < len) {
                            dArr[d++] = (byte)i;
                        }
                    }
                }

                return dArr;
            }
        }
    }

    public static final byte[] decodeFast(String s) {
        int sLen = s.length();
        if (sLen == 0) {
            return new byte[0];
        } else {
            int sIx = 0;

            int eIx;
            for(eIx = sLen - 1; sIx < eIx && IA[s.charAt(sIx) & 255] < 0; ++sIx) {
            }

            while(eIx > 0 && IA[s.charAt(eIx) & 255] < 0) {
                --eIx;
            }

            int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0;
            int cCnt = eIx - sIx + 1;
            int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;
            int len = ((cCnt - sepCnt) * 6 >> 3) - pad;
            byte[] dArr = new byte[len];
            int d = 0;
            int i = 0;
            int r = len / 3 * 3;

            while(d < r) {
                 i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)];
                 i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)];
                dArr[d++] = (byte)(i >> 16);
                dArr[d++] = (byte)(i >> 8);
                dArr[d++] = (byte)i;
                if (sepCnt > 0) {
                    ++i;
                    if (i == 19) {
                        sIx += 2;
                        i = 0;
                    }
                }
            }

            if (d < len) {
                i = 0;

                for(r = 0; sIx <= eIx - pad; ++r) {
                    i |= IA[s.charAt(sIx++)] << 18 - r * 6;
                }

                for(r = 16; d < len; r -= 8) {
                    dArr[d++] = (byte)(i >> r);
                }
            }

            return dArr;
        }
    }

    public static String decode(String s) throws UnsupportedEncodingException {
        return s == null ? null : new String(decodeFast(s), "UTF-8");
    }

    public String encodeSpecial(byte[] bt) {
        int totalBits = bt.length * 8;
        int nn = totalBits % 6;
        int curPos = 0;

        StringBuilder toReturn;
        for(toReturn = new StringBuilder(32); curPos < totalBits; curPos += 6) {
            int bytePos = curPos / 8;
            int pos;
            switch(curPos % 8) {
                case 0:
                    toReturn.append(PEM_ARRAY[(bt[bytePos] & 252) >> 2]);
                case 1:
                case 3:
                case 5:
                default:
                    break;
                case 2:
                    toReturn.append(PEM_ARRAY[bt[bytePos] & 63]);
                    break;
                case 4:
                    if (bytePos == bt.length - 1) {
                        toReturn.append(PEM_ARRAY[(bt[bytePos] & 15) << 2 & 63]);
                    } else {
                        pos = ((bt[bytePos] & 15) << 2 | (bt[bytePos + 1] & 192) >> 6) & 63;
                        toReturn.append(PEM_ARRAY[pos]);
                    }
                    break;
                case 6:
                    if (bytePos == bt.length - 1) {
                        toReturn.append(PEM_ARRAY[(bt[bytePos] & 3) << 4 & 63]);
                    } else {
                        pos = ((bt[bytePos] & 3) << 4 | (bt[bytePos + 1] & 240) >> 4) & 63;
                        toReturn.append(PEM_ARRAY[pos]);
                    }
            }
        }

        if (nn == 2) {
            toReturn.append("==");
        } else if (nn == 4) {
            toReturn.append("=");
        }

        return toReturn.toString();
    }

    public byte[] decodeSpecial(String str) throws IOException {
        byte[] arrayOfByte = str.getBytes();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(arrayOfByte);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        this.decodeBuffer(inputStream, outputStream);
        return outputStream.toByteArray();
    }

    private void decodeBuffer(InputStream paramInputStream, OutputStream paramOutputStream) throws IOException {
        PushbackInputStream localPushbackInputStream = new PushbackInputStream(paramInputStream);
        int j = 0;

        while(true) {
            while(true) {
                try {
                    int k = this.bytesPerLine();
                    int i = 0;
                    if (i + this.bytesPerAtom() < k) {
                        this.decodeAtom(localPushbackInputStream, paramOutputStream, this.bytesPerAtom());
                        j += this.bytesPerAtom();
                        int var10000 = i + this.bytesPerAtom();
                    } else if (i + this.bytesPerAtom() == k) {
                        this.decodeAtom(localPushbackInputStream, paramOutputStream, this.bytesPerAtom());
                        j += this.bytesPerAtom();
                    } else {
                        this.decodeAtom(localPushbackInputStream, paramOutputStream, k - i);
                        j += k - i;
                    }
                } catch (RuntimeException var7) {
                    String.valueOf(j);
                    return;
                }
            }
        }
    }

    private int bytesPerAtom() {
        return 4;
    }

    private int bytesPerLine() {
        return 72;
    }

    private void decodeAtom(PushbackInputStream paramPushbackInputStream, OutputStream paramOutputStream, int paramInt) throws IOException {
        int j = -1;
        int k = -1;
        int m = -1;
        int n = -1;
        if (paramInt < 2) {
            throw new ArrayStoreException("BASE64Decoder: Not enough bytes for an atom.");
        } else {
            int i;
            do {
                i = paramPushbackInputStream.read();
                if (i == -1) {
                    throw new RuntimeException();
                }
            } while(i == 10 || i == 13);

            this.decodeBuffer[0] = (byte)i;
            i = this.readFully(paramPushbackInputStream, this.decodeBuffer, 1, paramInt - 1);
            if (i == -1) {
                throw new RuntimeException();
            } else {
                if (paramInt > 3 && this.decodeBuffer[3] == 61) {
                    paramInt = 3;
                }

                if (paramInt > 2 && this.decodeBuffer[2] == 61) {
                    paramInt = 2;
                }

                switch(paramInt) {
                    case 4:
                        n = PEM_CONVERT_ARRAY[this.decodeBuffer[3] & 255];
                    case 3:
                        m = PEM_CONVERT_ARRAY[this.decodeBuffer[2] & 255];
                    case 2:
                        k = PEM_CONVERT_ARRAY[this.decodeBuffer[1] & 255];
                        j = PEM_CONVERT_ARRAY[this.decodeBuffer[0] & 255];
                    default:
                        switch(paramInt) {
                            case 2:
                                paramOutputStream.write((byte)(j << 2 & 252 | k >>> 4 & 3));
                                break;
                            case 3:
                                paramOutputStream.write((byte)(j << 2 & 252 | k >>> 4 & 3));
                                paramOutputStream.write((byte)(k << 4 & 240 | m >>> 2 & 15));
                                break;
                            case 4:
                                paramOutputStream.write((byte)(j << 2 & 252 | k >>> 4 & 3));
                                paramOutputStream.write((byte)(k << 4 & 240 | m >>> 2 & 15));
                                paramOutputStream.write((byte)(m << 6 & 192 | n & 63));
                        }

                }
            }
        }
    }

    private int readFully(InputStream paramInputStream, byte[] paramArrayOfByte, int paramInt1, int paramInt2) throws IOException {
        for(int i = 0; i < paramInt2; ++i) {
            int j = paramInputStream.read();
            if (j == -1) {
                return i == 0 ? -1 : i;
            }

            paramArrayOfByte[i + paramInt1] = (byte)j;
        }

        return paramInt2;
    }

    static {
        Arrays.fill(IA, -1);
        int i = 0;

        for(int iS = CA.length; i < iS; IA[CA[i]] = i++) {
        }

        IA[61] = 0;

        for(i = 0; i < 255; ++i) {
            PEM_CONVERT_ARRAY[i] = -1;
        }

        for(i = 0; i < PEM_ARRAY.length; ++i) {
            PEM_CONVERT_ARRAY[PEM_ARRAY[i]] = (byte)i;
        }

    }
}
